package com.ve.everything;

import com.ve.everything.searcher.Constants;
import com.ve.everything.searcher.LuceneIndex;
import org.apache.catalina.core.ApplicationContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.UUID;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) throws IOException, URISyntaxException {
        ConfigurableApplicationContext ac =  SpringApplication.run(DemoApplication.class, args);
        String fileindexPath = ac.getEnvironment().getProperty("fileindex");
        Constants.PATH = fileindexPath;
        Files.createDirectories(Paths.get(fileindexPath));
        Files.readAllLines(
                Paths.get(Thread.currentThread().getContextClassLoader().getResource("sqlxml.csv").toURI())
        ).forEach((lineString)->{
            String[] stringArray = lineString.split(",");
            File xmlfile = new File(stringArray[1],stringArray[0]);
            String[] pathArray = stringArray[1].split("\\\\");
//            System.out.println(pathArray[pathArray.length-1]);
            try {
                System.out.println(xmlfile.getAbsolutePath());
                LuceneIndex.indexDocument(
                        xmlfile,
                        new String[]{pathArray[ pathArray.length-1]}
                );
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        System.out.println("index done.");
    }
}
